/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.library;

import java.io.Serializable;

/**
 * Class, representing an objects version. A version is a pair of an replicas unique identifier and the replicas update
 * number associated with the version of the object.
 */
public class Version implements Serializable {
	private static final long serialVersionUID = 421425016585324901L;

	private String replica;
	private long version;

	/**
	 * Constructor.
	 * 
	 * @param replica Unique identifier of the replica that created this version.
	 * @param version Value of the global counter of the associated replica at the time this version is created.
	 */
	public Version(String replica, long version) {
		this.replica = replica;
		this.version = version;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Version other = (Version) obj;
		if (replica == null) {
			if (other.replica != null)
				return false;
		} else if (!replica.equals(other.replica))
			return false;
		if (version != other.version)
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((replica == null) ? 0 : replica.hashCode());
		result = prime * result + (int) (version ^ (version >>> 32));
		return result;
	}

	@Override
	public synchronized String toString() {
		return "<" + replica + "," + version + ">";
	}

	/**
	 * Get the associated replicas unique name.
	 * 
	 * @return The associated replicas unique name
	 */
	public String getReplica() {
		return replica;
	}

	/**
	 * Set the associated replicas unique name.
	 * 
	 * @param replica The associated replicas unique name
	 */
	public void setReplica(String replica) {
		this.replica = replica;
	}

	/**
	 * Get the update number.
	 * 
	 * @return Version number
	 */
	public long getVersion() {
		return version;
	}

	/**
	 * Set version number.
	 * 
	 * @param version Version number
	 */
	public void setVersion(long version) {
		this.version = version;
	}
}